package org.example.wu_and_dian.service.impl;

import org.example.wu_and_dian.dto.request.DocumentRequest;
import org.example.wu_and_dian.dto.request.UpdateDocument;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.dto.response.DocumentResponse;
import org.example.wu_and_dian.mapper.DocumentMapper;
import org.example.wu_and_dian.entity.Document;
import org.example.wu_and_dian.service.DocumentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document> implements DocumentService {

    @Override
    public AjaxResult updateDocumentContent(String documentId, String content, Date updatedAt) {
        Document document = documentMapper.selectById(documentId);
        if (document == null) {
            return AjaxResult.error("文档不存在");
        }
        document.setContent(content);
        document.setUpdatedAt(updatedAt);
        int updatedCount = documentMapper.updateContentById(document);
        return updatedCount > 0 
            ? AjaxResult.success("更新文档内容成功")
            : AjaxResult.error("更新失败");
    }

    @Override
    public AjaxResult createDocument(DocumentRequest document) {
        if (document.getProjectId() == null || document.getProjectId().isEmpty()) {
            return AjaxResult.error("项目ID不能为空");
        }
        if (document.getName() == null || document.getName().isEmpty()) {
            return AjaxResult.error("文档名称不能为空");
        }
        Document entity = new Document();
        entity.setId(document.getDocumentId());
        entity.setProjectId(document.getProjectId());
        entity.setName(document.getName());
        entity.setDescription(document.getDescription());
        entity.setCreatedAt(document.getCreatedAt());
        entity.setUpdatedAt(document.getUpdatedAt());
        entity.setContent(document.getContent());
        try {
            documentMapper.insertDocument(entity);
            return new AjaxResult().put("success", true).put("documentId", entity.getId()).put("message", "创建新文档成功");
        } catch (Exception e) {
            log.error("文档创建失败: " + e.getMessage());
            return new AjaxResult().put("success", false).put("message", "文档创建失败: " + e.getMessage());
        }
    }


    @Autowired
    DocumentMapper documentMapper;
    public List<DocumentResponse> getDocumentsByProjectId(String projectId) {
        List<Document> documents = documentMapper.selectDocumentsByProjectId(projectId);
        System.out.println(documents);
        List<DocumentResponse> documentResponses = new ArrayList<>();
        documents.forEach(document -> {
            DocumentResponse documentResponse = new DocumentResponse();
            documentResponse.setId(document.getId());
            documentResponse.setName(document.getName());
            documentResponse.setDescription(document.getDescription());
            documentResponse.setCreatedAt(document.getCreatedAt());
            documentResponse.setUpdatedAt(document.getUpdatedAt());
            documentResponses.add(documentResponse);
        });
        // 调用DocumentMapper的方法进行查询
        return documentResponses;
    }

    @Override
    public int deleteDocument(String documentId) {
        return documentMapper.deleteById(documentId);
    }

    @Override
    public AjaxResult updateDocument(UpdateDocument updateDocument) {
        int updatedCount = documentMapper.updateDocument(updateDocument);
        if (updatedCount > 0){
            return AjaxResult.success("更新成功");
        }else {
            return AjaxResult.error("更新失败，未知错误");
        }
    }

    @Override
    public AjaxResult uploadDocument(DocumentRequest document) {
        if (document.getProjectId() == null || document.getProjectId().isEmpty()) {
            return AjaxResult.error("项目ID不能为空");
        }
        if (document.getName() == null || document.getName().isEmpty()) {
            return AjaxResult.error("文档名称不能为空");
        }
        Document entity = new Document();
        entity.setId(document.getDocumentId());
        entity.setProjectId(document.getProjectId());
        entity.setName(document.getName());
        entity.setDescription(document.getDescription());
        entity.setCreatedAt(document.getCreatedAt());
        entity.setUpdatedAt(document.getUpdatedAt());
        entity.setContent(document.getContent());
        try {
            documentMapper.insertDocument(entity);
            return new AjaxResult().put("success", true).put("documentId", entity.getId()).put("message", "新文档" +
                    "上传成功");
        } catch (Exception e) {
            log.error("文档上传失败: " + e.getMessage());
            return new AjaxResult().put("success", false).put("message", "文档上传失败: " + e.getMessage());
        }
    }

    @Override
    public Document getDocument(String documentId) {
        return documentMapper.selectById(documentId);
    }

    @Override
    public int updateContentAuthorById(Document document) {
        return documentMapper.updateContentAuthorById(document);
    }
}